<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>商品库存详情</span>
      </div>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input
            v-model="queryParams.goodsName"
            placeholder="请输入商品名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="商品分类" prop="categoryId">
          <el-select v-model="queryParams.categoryId" placeholder="请选择商品分类" clearable style="width: 240px">
            <el-option
              v-for="item in categoryOptions"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="库存状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择库存状态" clearable style="width: 240px">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            @click="handleExport"
            v-hasPermi="['inventory:stock:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <!-- 库存详情表格 -->
      <el-table
        v-loading="loading"
        :data="stockList"
        row-key="id"
        border
      >
        <el-table-column label="商品编码" align="center" prop="goodsCode" />
        <el-table-column label="商品名称" align="center" prop="goodsName" :show-overflow-tooltip="true" />
        <el-table-column label="规格型号" align="center" prop="goodsSpec" />
        <el-table-column label="单位" align="center" prop="goodsUnit" />
        <el-table-column label="当前库存" align="center" prop="currentStock">
          <template slot-scope="scope">
            <el-tag :type="getStockTagType(scope.row)">{{ scope.row.currentStock }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="最低库存" align="center" prop="minStock" />
        <el-table-column label="最高库存" align="center" prop="maxStock" />
        <el-table-column label="锁定库存" align="center" prop="lockedStock" />
        <el-table-column label="在途库存" align="center" prop="transitStock" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === '0' ? 'success' : 'info'">
              {{ scope.row.status === '0' ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { listStock, exportStock } from "@/api/inventory/stock";
import { listCategory } from "@/api/stock/category";

export default {
  name: "StockDetails",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存列表
      stockList: [],
      // 商品分类选项
      categoryOptions: [],
      // 状态选项
      statusOptions: [
        { dictLabel: "正常", dictValue: "0" },
        { dictLabel: "停用", dictValue: "1" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        categoryId: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.getList();
    this.getCategoryOptions();
  },
  methods: {
    /** 查询库存列表 */
    getList() {
      this.loading = true;
      console.log("开始获取库存详情数据...");
      listStock(this.queryParams).then(response => {
        console.log("获取库存数据响应:", response);
        if (response.rows) {
          this.stockList = response.rows;
          this.total = response.total;
        } else {
          this.stockList = [];
          this.total = 0;
          this.$message.warning("未获取到库存数据");
        }
        this.loading = false;
      }).catch(err => {
        console.error("获取库存数据失败:", err);
        this.loading = false;
        this.$message.error("获取库存数据失败，请检查网络连接或联系管理员");
      });
    },
    /** 获取商品分类下拉选项 */
    getCategoryOptions() {
      listCategory().then(response => {
        this.categoryOptions = response.data || [];
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有库存数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$message.success("正在导出库存数据，请稍候...");
        return exportStock(this.queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(() => {});
    },
    /** 根据库存情况返回标签类型 */
    getStockTagType(row) {
      if (!row.minStock) return '';
      
      if (row.currentStock < row.minStock) {
        return 'danger';  // 库存不足
      } else if (row.maxStock && row.currentStock > row.maxStock) {
        return 'warning';  // 库存过多
      } else {
        return 'success';  // 库存正常
      }
    }
  }
};
</script>

<style scoped>
.el-tag {
  min-width: 60px;
}
</style> 